package com.cyt.web.controller.base;

import com.cyt.base.util.StringUtils;
import com.cyt.work.wb.model.po.WbUser;
import com.cyt.work.wb.service.WbUserService;
import com.cyt.wx.model.po.WxEducationUser;
import com.cyt.wx.model.po.WxMember;
import com.cyt.wx.service.WxEducationUserService;
import com.cyt.wx.service.WxMemberService;
import com.github.pagehelper.StringUtil;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: YuShengQiang
 * @Date: 2018-11-16 17:17
 * @Description: 手机端控制层基础方式
 */
public class PhoneController {

    /**
     * 社团招新应用标识
     */
    public static final String APPLY_TYPE_STZX = "1";

    public static final String APPLY_STZX_LOGIN_URL = "redirect:/phone/associationnew/associationLogin?token=%s&applyId=%s";

    /**
     * 问卷应用标识
     */
    public static final String APPLY_TYPE_QUESTION = "2";

    /**
     * 维保应用标识
     */
    public static final String APPLY_TYPE_WB = "3";

    /**
     * 考务应用标识
     */
    public static final String APPLY_TYPE_KW = "4";
    /**
     * 分班应用标识
     */
    public static final String APPLY_TYPE_FB = "5";

    @Resource
    private WxEducationUserService wxEducationUserService;
    @Resource
    private WbUserService wbUserService;
    @Resource
    private WxMemberService wxMemberService;

    /**
     * @Author: YuShengQiang
     * @Date: 2018-11-16 17:18
     * @Description: 用户token通用获取
     */
    protected WxMember acceptToken(String token) {
        if (StringUtil.isNotEmpty(token)) {
            WxMember wxMember = wxMemberService.selectMemberByToken(token);
            if (wxMember != null) {
                return wxMember;
            }
        }
        return null;
    }

    /**
     * 用户token通用获取并验证登录
     *
     * @param token 用户token值
     * @param applyId 应用id
     * @param applyType 需要登录验证的应用标识,1、社团招新2、问卷调查,3、维保
     * @return
     * @author shengQiang yu
     * @date 2019-09-03 14:57
     */
    protected Map<String, Object> acceptToken(String token, String applyId, String applyType) {
        if (StringUtil.isNotEmpty(token)) {
            Map<String, Object> map = new HashMap<>();
            WxMember wxMember = wxMemberService.selectMemberByToken(token);
            if (wxMember != null) {
                if (StringUtils.equals(applyType, APPLY_TYPE_STZX)) {
                    return checkLoginStzx(wxMember, applyId);
                } else if (StringUtils.equals(applyType, APPLY_TYPE_QUESTION)) {
                    return checkLoginQuestion(wxMember, applyId);
                }else if (StringUtils.equals(applyType, APPLY_TYPE_WB)) {
                    return checkLoginWb(wxMember, applyId);
                }else if (StringUtils.equals(applyType, APPLY_TYPE_KW)) {
                    return checkLoginKw(wxMember, applyId);
                }else if(StringUtils.equals(applyType, APPLY_TYPE_FB)){
                    return checkLoginFb(wxMember, applyId);
                }
            }
            map.put("wxMember", wxMember);
            map.put("flag", false);
            return map;
        }
        return null;
    }

    /**
     * 问卷登录校验
     * @param wxMember
     * @param applyId
     * @author congcong tao
     * @date 2019/9/4 16:09
     */
    private Map<String, Object> checkLoginQuestion(WxMember wxMember, String applyId) {
        // 查询用户是否在education表中,主要根据applyId查询创建者
        boolean flag = wxEducationUserService.checkByMemberId(wxMember.getId());
        Map<String, Object> map = new HashMap<>();
        map.put("flag", flag);
        map.put("wxMember", wxMember);
        map.put("modelAndView", new ModelAndView("redirect:/phone/question/questionLogin?token=" + wxMember.getToken() + "&applyId=" + applyId));
        return map;
    }

    /**
     * 社团招新登录验证
     *
     * @param wxMember 会员
     * @param applyId 应用id
     * @return
     * @author shengQiang yu
     * @date 2019-09-03 15:04
     */
    private Map<String, Object> checkLoginStzx(WxMember wxMember, String applyId){
        WxEducationUser wxEducationUser = wxEducationUserService.selectEducationUserByMemberId(wxMember.getId());
        Map<String, Object> map = new HashMap<>();
        if (wxEducationUser == null) {
            map.put("flag", false);
        } else {
            map.put("flag", true);
            map.put("wxEducationUser", wxEducationUser);
        }
        map.put("wxMember", wxMember);
        map.put("modelAndView", new ModelAndView("redirect:/phone/association/associationLogin?token=" + wxMember.getToken() + "&applyId=" + applyId));
        return map;
    }



    /**
     * 考务安排登录
     *
     * @param wxMember 会员
     * @param applyId 应用id
     * @return
     * @author shengQiang yu
     * @date 2019-09-03 15:04
     */
    private Map<String, Object> checkLoginKw(WxMember wxMember, String applyId){
        WxEducationUser wxEducationUser = wxEducationUserService.selectEducationUserByMemberId(wxMember.getId());
        Map<String, Object> map = new HashMap<>();
        if (wxEducationUser == null) {
            map.put("flag", false);
        } else {
            map.put("flag", true);
            map.put("wxEducationUser", wxEducationUser);
        }
        map.put("wxMember", wxMember);
        map.put("modelAndView", new ModelAndView("redirect:/phone/kwmanagement/login?token=" + wxMember.getToken() + "&applyId=" + applyId));
        return map;
    }
    /**
     * 分班安排登录
     *
     * @param wxMember 会员
     * @param applyId 应用id
     * @return
     * @author shengQiang yu
     * @date 2019-09-03 15:04
     */
    private Map<String, Object> checkLoginFb(WxMember wxMember, String applyId){
        WxEducationUser wxEducationUser = wxEducationUserService.selectEducationUserByMemberId(wxMember.getId());
        Map<String, Object> map = new HashMap<>();
        if (wxEducationUser == null) {
            map.put("flag", false);
        } else {
            map.put("flag", true);
            map.put("wxEducationUser", wxEducationUser);
        }
        map.put("wxMember", wxMember);
        map.put("modelAndView", new ModelAndView("redirect:/phone/divideclass/divideclassLogin?token=" + wxMember.getToken() + "&applyId=" + applyId));
        return map;
    }
    /**
     * 维保登录验证
     *
     * @param wxMember 会员
     * @param applyId 应用id
     * @return
     * @author shengQiang yu
     * @date 2019-09-03 15:04
     */
    private Map<String, Object> checkLoginWb(WxMember wxMember, String applyId){
        WbUser wbUser=wbUserService.selectOne(new WbUser(wxMember.getId()));
        Map<String, Object> map = new HashMap<>();
        if (wbUser == null) {
            map.put("flag", false);
        } else {
            map.put("flag", true);
            map.put("wbUser", wbUser);
        }
        map.put("wxMember", wxMember);
        map.put("modelAndView", new ModelAndView("redirect:/phone/wb/login?token=" + wxMember.getToken() + "&applyId=" + applyId));
        return map;
    }

}
